function serializeArray(a)
{
	var arrayLength = 0;
	var r = '';
	for(var aKey in a)
	{
		if(arrayLength == 0) {
			r += 'order[]=';
		}
		else {
			r += '&order[]=';
		}
		arrayLength++;
		r += a[aKey];
	}
	return r;
}
function addClose () {
	$('.message').prepend('<span class="close-box"><a href="javascript:;">close</a></span>');
	$('.close-box').click(function () {
		var parent = $(this).parent();
		parent = $(parent).parent();
		$(parent).slideUp();
	});
}
$(document).ready(function() {
	addClose();
	$('.close-box').click(function () {
		var parent = $(this).parent();
		parent = $(parent).parent();
		$(parent).slideUp();
	});
	$('.toggleParent').click(function () {
		var parent = $(this).parent();
		$(parent).slideUp();
	});
	$('fieldset:not(#cms-fieldset-login)').append('<a href="#header" class="back-top">^ back to top</a>');
	$("a.delete-user").click(function() {
		if(document.getElementById('disable-warnings-checkbox').checked === true) {
			var x = $(this).parent().parent();
			$.ajax({
				type: "POST",
				url: "admin.php?page=user-management",
				data: "name="+$(x).children('.username').text(),
				success: function(responseText){
					$('body').append("<div class='floating-box'>"+responseText+"</div>");
					$('.floating-box').hide().fadeIn('slow').animate({opacity: 1}, 3000).fadeOut('slow');
					$(x).fadeOut('slow');
				}		
			});
		}
		else {
			var r = confirm("Are you sure you want to delete this user?");
			if(r === true) {
				var x = $(this).parent().parent();
				$.ajax({
					type: "POST",
					url: "admin.php?page=user-management",
					data: "name="+$(x).children('.username').text(),
					success: function(responseText){
						$('body').append("<div class='floating-box'>"+responseText+"</div>");
						$('.floating-box').hide().fadeIn('slow').animate({opacity: 1}, 3000).fadeOut('slow');
						$(x).fadeOut('slow');
					}		
				});
			}
			else {
				
			}
		}
	});
	$("span.delete-page a").click(function() {
		if(document.getElementById('disable-warnings-checkbox').checked === true) {
			var x = $(this).parent().parent().parent();
				$.ajax({
					type: "POST",
					url: "admin.php?page=edit-and-delete-pages",
					data: "deleteid="+$(x).children('.admin-edit-link').attr('id'),
					success: function(responseText){
						$('body').append("<div class='floating-box'>"+responseText+"</div>");
						$('.floating-box').hide().fadeIn('slow').animate({opacity: 1}, 3000).fadeOut('slow');
						$(x).fadeOut('slow');
					}		
				});
		}
		else {
			var r = confirm("Are you sure you want to delete this page? If this is a parent item, ALL SUB-PAGES WILL BE DELETED!");
			if(r === true) {
				var x = $(this).parent().parent();
				$.ajax({
					type: "POST",
					url: "admin.php?page=edit-and-delete-pages",
					data: "deleteid="+$(x).children('.admin-edit-link').attr('id'),
					success: function(responseText){
						$('body').append("<div class='floating-box'>"+responseText+"</div>");
						$('.floating-box').hide().fadeIn('slow').animate({opacity: 1}, 3000).fadeOut('slow');
						$(x).fadeOut('slow');
					}		
				});
			}
			else {
				
			}
		}
	});
	$('body').append('<div class="tooltip"></div>');
	$('#static-content tr, .helplink').hover(function (e) {
		$('.tooltip').html($(this).attr('title')).css({top:e.pageY,left:e.pageX}).show();
		$(this).mousemove(function (e) {
			$('.tooltip').css({top:e.pageY,left:e.pageX});
			if (window.innerWidth) { //if browser supports window.innerWidth
				var w = window.innerWidth;
				var h = window.innerHeight;
			}
			else if (document.all) { //else if browser supports document.all (IE 4+)
				var w = document.body.clientWidth;
				var h = document.body.clientHeight;
			}
			var enw =$('.tooltip').css('left').split('px');
			enw = enw[0];
			enw = Number(enw) + 150;
			if (enw > w) {
				var nt = enw - 250;
				$('.tooltip').css('left', nt+'px');
			}
			return false;
		});
	}, function () {
		$('.tooltip').hide();
	});
	$("input").focus(function() {
		// only select if the text has not changed
		$(this).toggleClass('focus');
		if(this.value === this.defaultValue)	{
			this.select();
		}
	});
	$("input, textarea").blur(function() {
		// only select if the text has not changed
		$(this).toggleClass('focus');
	});
	$("input[type^=submit], button").addClass('button');
	$("input, button").hover(function() {
		$(this).toggleClass('hover');
	},
		function () {
			$(this).toggleClass('hover');
		}
	);
	//$("#users").tablesorter( {sortList: [[3,0], [0,0]]} );
	//$("table").tablesorter();
	$("#username").alphanumeric({nocaps:true});
	$("#theme-name").alphanumeric();
	$(".alphanumeric").alphanumeric({allow:' '});
	$("table tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
	$("table tr:even").addClass("alt");
	$(".edit-file").click(function() {
		var href = $(this).attr('title');
		$('.expand-textbox').css('visibility', 'visible');
		$.get('admin.php?page=theme-management&tpage='+href, false, function (responseText) {
			$('.expand-textbox').css('visibility', 'hidden');
			document.getElementById('editTheme').value = responseText;
		});
		
		$('#editing-file-head span').text(href);
		return false;
	});
	$("#formEditTheme").submit(function () {
		$.ajax({
			type: 'POST',
			data: 'tpage='+$('#editing-file-head span').text()+'&content='+encodeURIComponent(document.getElementById('editTheme').value),
			url: 'admin.php?page=theme-management',
			success: function(responseText) {
				$('.msg-box').html(responseText);
				addClose();
			}
		});
		return false;
	});
});